﻿#region using
using System;
using System.Xml;
#endregion

namespace XmlGenSharp.Lib.Utility
{
  #region Class Docs
  /// <summary>
  /// Summary description for XmlUtil.
  /// </summary>
  #endregion

  public class XmlUtil
  {
    // Constructors

    #region XmlUtil()
    protected XmlUtil()
    {
    }
    #endregion

    // Public Static Methods

    #region AddXmlElement(XmlDocument xml, XmlNode parent, string nodeName, string nodeText)
    public static void AddXmlElement(XmlDocument xml, XmlNode parent, string nodeName, string nodeText)
    {
      XmlElement node = xml.CreateElement(nodeName);
      node.InnerText = nodeText;
      parent.AppendChild(node);
    }
    #endregion

    #region LoadString(ref string val, XmlNode parent, string nodeName)
    public static void LoadString(ref string val, XmlNode parent, string nodeName)
    {
      try
      {
        XmlElement elem = parent[nodeName];
        if (elem != null)
          val = elem.InnerText.Trim();
      }
      catch
      {
        val = string.Empty;
      }
    }
    #endregion

    #region LoadBool(ref bool val, XmlNode parent, string nodeName)
    public static void LoadBool(ref bool val, XmlNode parent, string nodeName)
    {
      try
      {
        XmlElement elem = parent[nodeName];
        if (elem != null)
          val = (elem.InnerText.Trim() == "1");
      }
      catch
      {
        val = false;
      }
    }
    #endregion

    #region LoadInt(ref int val, XmlNode parent, string nodeName)
    public static void LoadInt(ref int val, XmlNode parent, string nodeName)
    {
      try
      {
        XmlElement elem = parent[nodeName];
        if (elem != null)
          val = Int32.Parse(elem.InnerText.Trim());
      }
      catch
      {
        val = 0;
      }
    }
    #endregion

    #region GetAttrib(XmlNode node, string name)
    public static string GetAttrib(XmlNode node, string name)
    {
      if (node.Attributes == null)
        return null;

      if (node.Attributes[name] == null)
        return null;

      return node.Attributes[name].InnerText;
    }
    #endregion

    #region GetIntAttrib(XmlNode node, string name, out bool found, out bool valid, ref int result)
    public static void GetIntAttrib(XmlNode node, string name, out bool found, out bool valid, ref int result)
    {
      if (node.Attributes == null || node.Attributes[name] == null)
      {
        found = false;
        valid = false;
        return;
      }

      found = true;
      valid = Int32.TryParse(node.Attributes[name].InnerText, out result);
    }
    #endregion

    #region GetAttribOrThrow(XmlNode node, string attrib)
    public static string GetAttribOrThrow(XmlNode node, string attrib)
    {
      string val = GetAttrib(node, attrib, null);
      if (val == null)
        throw new Exception(String.Format("Attribute '{0}' not specified in node '{1}'", attrib, node.Name));

      return val;
    }
    #endregion

    #region GetAttrib(XmlNode node, string attrib, string defVal)
    public static string GetAttrib(XmlNode node, string attrib, string defVal)
    {
      XmlAttribute xmlAttrib = node.Attributes[attrib];
      if (xmlAttrib == null)
        return defVal;

      string val = xmlAttrib.Value;
      return (val == null) ? defVal : val;
    }
    #endregion

    #region GetBoolAttrib(XmlNode node, string attrib, bool defVal)
    public static bool GetBoolAttrib(XmlNode node, string attrib, bool defVal)
    {
      XmlAttribute xmlAttrib = node.Attributes[attrib];
      if (xmlAttrib == null)
        return defVal;

      string val = xmlAttrib.Value;
      if (val == null || val == string.Empty)
        return defVal;

      bool returnVal = (val == "1" || val.ToLower() == "true");

      return returnVal;
    }
    #endregion

    #region GetBoolAttribOrThrow(XmlNode node, string attrib)
    public static bool GetBoolAttribOrThrow(XmlNode node, string attrib)
    {
      string val = GetAttrib(node, attrib, null);
      if (val == null)
        throw new Exception(String.Format("Attribute '{0}' not specified in node '{1}'", attrib, node.Name));

      if (val == null || val == string.Empty)
        return false;

      bool returnVal = (val == "1" || val.ToLower() == "true");

      return returnVal;
    }
    #endregion

    #region GetIntAttrib(XmlNode node, string attrib, int defVal)
    public static int GetIntAttrib(XmlNode node, string attrib, int defVal)
    {
      XmlAttribute xmlAttrib = node.Attributes[attrib];
      if (xmlAttrib == null)
        return defVal;

      string val = xmlAttrib.Value;
      if (val == null || val == string.Empty)
        return defVal;

      int returnVal = defVal;
      int.TryParse(val, out returnVal);

      return returnVal;
    }
    #endregion

    #region GetIntAttribOrThrow(XmlNode node, string attrib)
    public static int GetIntAttribOrThrow(XmlNode node, string attrib)
    {
      string val = GetAttrib(node, attrib, null);
      if (val == null)
        throw new Exception(String.Format("Attribute '{0}' not specified in node '{1}'", attrib, node.Name));

      if (val == null || val == string.Empty)
        return 0;

      int returnVal = 0;
      int.TryParse(val, out returnVal);

      return returnVal;
    }
    #endregion

    #region GetInt64Attrib(XmlNode node, string attrib, Int64 defVal)
    public static Int64 GetInt64Attrib(XmlNode node, string attrib, Int64 defVal)
    {
      XmlAttribute xmlAttrib = node.Attributes[attrib];
      if (xmlAttrib == null)
        return defVal;

      string val = xmlAttrib.Value;
      if (val == null || val == string.Empty)
        return defVal;

      Int64 returnVal = defVal;
      Int64.TryParse(val, out returnVal);

      return returnVal;
    }
    #endregion

    #region GetInt64AttribOrThrow(XmlNode node, string attrib)
    public static Int64 GetInt64AttribOrThrow(XmlNode node, string attrib)
    {
      string val = GetAttrib(node, attrib, null);
      if (val == null)
        throw new Exception(String.Format("Attribute '{0}' not specified in node '{1}'", attrib, node.Name));

      if (val == null || val == string.Empty)
        return 0;

      Int64 returnVal = 0;
      Int64.TryParse(val, out returnVal);

      return returnVal;
    }
    #endregion
  }
}

